# -*- coding: utf-8 -*-
"""
Created on Thu Dec 17 16:00:57 2020

@author: 薛莹莹
"""
from pymprog import *

# 数据
a = ('A1', 'A2', 'A3')        # （物资库）（此处可以读入经纬度）
b = ('B1', 'B2', 'B3', 'B4')  # （需求点）（同上）
# （物资库）,（需求点）):运价
c = [[6, 2, 6, 5],
     [4, 9, 5, 2],
     [5, 2, 1, 3],
     ]
price = dict() #属于写个字典
for i in a:
	for j in b:
		price[i, j] = c[a.index(i)][b.index(j)]

# 物资库物资数量
d = [60, 55, 51]
produce = dict()
for i in a:
	produce[i] = d[a.index(i)]

# 需求点需求的物资量
e = [35, 37, 72, 22]#可用读入 excel，txt的方法
put = dict()
for i in b:
	put[i] = e[b.index(i)]

print("(物资库,需求点):运价\n", price)
print("\n物资库:物资量\n", produce)
print("\需求点:需求量\n", put)

# 模型及求解
begin('transport')
x = var('x', price.keys())                                             # 调运方案
minimize(sum(price[i, j]*x[i, j] for (i, j) in price.keys()), 'Cost')  # 总运费最少
for i in produce.keys():                                               # 物资量约束
    sum(x[i, j] for j in put.keys()) == produce[i]
for j in put.keys():                                                   # 需求量约束
    sum(x[i ,j] for i in produce.keys()) == put[j]

def report():
    print("\n调运方案(最优之一)")
    for (i, j) in price.keys():
        if x[i, j].primal > 0 and price[i, j] != 0:
            print("物资库:%s -> 需求点:%s 运输量:%-2d 运价:%2d" % (i, j, int(x[i, j].primal), int(price[i, j])))
            #-2d往后空格，2d数字前空格。
    print("总费用:%d"%int(vobj()))

solve()
report()
end()


